<template>
  <div>
    <div class="ser_div">
      <el-row>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 产品 </el-col>
            <el-col :span="16">
              <el-select v-model="ser.product_id" filterable  clearable  @change="product_id_change" placeholder="请选择产品"
                style="width: 100%">
                <el-option v-for="(item, index) in productList" :key="index" :label="item.product_name" :value="item.id">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-select filterable clearable v-model="ser.model_number_id" placeholder="请选择型号" style="width: 100%">
            <el-option v-for="(item, index) in ModelNumberList" :key="index" :label="item.model_number" :value="item.id">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button @click="model_number_click" style="margin-left: 15px; background: #1890ff; color: aliceblue">
            搜索</el-button>
          <el-button @click="add_model_number" style="background: #1890ff; color: aliceblue">新增型号</el-button>
          <el-button @click="chongzhi" style="background: #1890ff; color: aliceblue">重置</el-button>

        </el-col>
      </el-row>
    </div>
    <el-table :data="dataList" stripe style="width: 100%" border>
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column label="产品名称">
        <template slot-scope="scope">
          <span style='display: block;'>{{ scope.row.product[0] ? scope.row.product[0].product_name : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="型号">
        <template slot-scope="scope">
          <span style="display: block">{{ scope.row.model_number }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img :src="uploadImgUrl + scope.row.file.file_url" @click="push_img(scope.row)" style="
              cursor: pointer;
              display: block;
              width: 60px;
              display: block;
              margin: 0 auto;
            " v-if="scope.row.file != null" />
          <img src="../../../src/assets/zwtp.jpg" style="width: 60px; display: block; margin: 0 auto" v-else />
        </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">
          <span style="display: block">{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="set_user(scope.row)" type="text" style="margin-right: 15px">关联工人</el-button>
          <el-button @click="set_product(scope.row)" type="text" style="margin-right: 15px">编辑</el-button>
          <el-popconfirm confirm-button-text="确定" cancel-button-text="取消" icon="el-icon-info" icon-color="red"
            title="是否删除这条数据？" @confirm="del_product(scope.row)">
            <el-button slot="reference" type="text">
              删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background @prev-click="prev"
        @next-click="next" :current-page.sync="pagination.page" :page-size="pagination.per_page"
        layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="isProduct" width="40%" :close-on-click-modal="false" @close="close_diolog">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="关联产品" prop="product_ids">
          <el-select v-model="ruleForm.product_ids" filterable multiple placeholder="请选择产品" style="width:50%;">
            <el-option v-for="item in productList" :key="item.id" :label="item.product_name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="型号名称" prop="model_number">
          <el-input v-model="ruleForm.model_number" style="width:50%;"></el-input>
        </el-form-item>
        <el-form-item label="型号图片" prop="file_id">
          <uplodeImg ref="Upload" :value="imgUrl" :limit="limit" @subImg="subImg"></uplodeImg>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" style="width:50%;"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd('ruleForm')">确定</el-button>
      </span>
    </el-dialog>
    <bigimg :imgurl="bigurl" :product="bigproduct" :model_number="bigmodel_number" ref="img_big"></bigimg>


    <el-dialog title="工人绑定" :visible.sync="isProduct_user" width="40%" :close-on-click-modal="false"
      @close="close_diolog_bangd">
      <div class="list_order" style="margin-bottom: 5px;">
        <div class="lis">
          <el-row style="display: flex;align-items: center;">
            <el-col>产品： <span style="font-weight: bold;"> {{ bangding.product }}</span></el-col>
            <el-col>型号 ：<span style="font-weight: bold;">{{ bangding.model_number }}</span></el-col>
          </el-row>
        </div>
      </div>
      <div class="list_order">
        <div class="lis" v-for="item, index in add_user" :key="index">
          <el-row style="display: flex;align-items: center;">
            <el-col :span="12" style="display: flex;align-items: center;">工种：<el-input v-model="item.job_type" disabled
                style="flex: 1;margin-right: 15px;"></el-input></el-col>
            <el-col :span="12" style="display: flex;align-items: center;">工人：
              <el-select v-model="item.users" multiple placeholder="请选择工人：" style="flex: 1;margin-right: 15px;">
                <el-option v-for="ite, inde in item.job_type_user" :label="ite.employee_name" :value="ite.user_id"
                  :key="ite.id">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct_user = false">取 消</el-button>
        <el-button type="primary" @click="set_ModelNumberUser">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getModelNumberList,
  addModelNumber,
  delModelNumber,
  getProductList,
  getProductJobTypeList,
  getEmployeeList,
  setModelNumberUser,
  getModelNumberUser
} from "@/api/product/index";
import uplodeImg from "@/components/ImageUpload";
import bigimg from "@/components/bigimg";
import { Result } from "element-ui";
export default {
  components: {
    uplodeImg,
    bigimg,
  },
  data() {
    return {
      ser: {
        product_id: "",
        model_number_id: "",
        file:1
      },
      bigurl: "",
      bigproduct: "",
      bigmodel_number: "",
      uploadImgUrl: this.$global.imgQianJui,
      limit: 1,
      imgUrl: "",
      model_number: "",
      isprice: false,
      price: "",
      title: "",
      isProduct: false,
      dataList: [],
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      ruleForm: {
        id: "",
        model_number: "",
        product_ids: [],
        file_id: "",
        remark: '',
      },
      rules: {
        product_ids: [
          { required: true, message: "请选择产品", trigger: "blur" },
        ],
        model_number: [
          { required: true, message: "请输入型号名称", trigger: "blur" },
        ]
      },
      specificationlist: [],
      productList: [],
      ModelNumberList: [],
      producttypelist: [],
      isProduct_user: false,
      add_user: [],
      bangding: {
        product: '',
        model_number: ''
      }
    };
  },
  mounted() {
    this.getlist();
    this.getProduct();
  },
  methods: {
    chongzhi(){
      this.ser={
        product_id: "",
        model_number_id: "",
        file: 1
      }
      this.getlist();
    },
    set_ModelNumberUser() {




      setModelNumberUser({ param_data: this.add_user }).then(res => {
        if (res.code == 200) {
          this.isProduct_user = false
          this.close_diolog_bangd()
        }
      })
    },

    close_diolog_bangd() {
      this.bangding = {
        product: '',
        model_number: ''
      }
      this.add_user = []
    },
    async get_ModelNumberUser(id, ids) {
      var istrue = true
      await getModelNumberUser({ model_number_id: id, product_id: ids }).then(res => {
        if (res.code == 200) {
          if (res.data.length > 0) {
            res.data.forEach(element => {
              let obj = {
                users: element.user_id_arr,
                job_type_user: [],
                model_number_id: id,
                job_type_id: element.job_type.id,
                job_type: element.job_type.job_type
              }
              this.get_EmployeeList(element.job_type.id).then(Result => {
                obj.job_type_user = Result
                console.log(Result)
              })
              this.add_user.push(obj)
              this.isProduct_user = true
            });
          } else {
            istrue = false
          }
        }
      })
      return istrue
    },
    set_user(row) {
      this.bangding.product = row.product[0].product_name
      this.bangding.model_number = row.model_number
      this.get_ModelNumberUser(row.id, row.product[0].id).then(Result => {
        if (Result == true) {
          return false
        } else {
          getProductJobTypeList({ product_id: row.product[0].id }).then(res => {
            if (res.code == 200) {
              this.isProduct_user = true
              res.data.forEach(element => {
                let obj = {
                  users: [],
                  job_type_user: [],
                  model_number_id: row.id,
                  job_type_id: element.id,
                  job_type: element.job_type
                }
                this.get_EmployeeList(element.id).then(Result => {
                  obj.job_type_user = Result
                  console.log(Result)
                })
                this.add_user.push(obj)
              });
            }
          })
        }
      })


    },
    async get_EmployeeList(id) {
      var arry = []
      await getEmployeeList({ job_type_id: id, per_page: 999 }).then(res => {
        if (res.code == 200) {
          arry = res.data.data
        }
      })
      return arry
    },
    product_id_change(val) {
      this.ModelNumberList = [];
      this.ser.model_number_id = "";
      this.get_producModel(val);
    },
    get_producModel(id) {
      getModelNumberList({ product_id: id, field_list: 'id,model_number', is_dropdown: 1 }).then((res) => {
        if (res.code == 200) {
          this.ModelNumberList = res.data.data;
        }
      });
    },
    push_img(row) {
      this.bigurl = row.file.file_url;
      this.bigproduct = row.product[0].product_name;
      this.bigmodel_number = row.model_number.model_number;
      this.$refs.img_big.open();
    },
    subImg(id) {
      this.ruleForm.file_id = id;
      console.log(id);
    },
    //获取产品
    getProduct() {
      getProductList().then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;
        }
      });
    },
    set_product(row) {
      console.log(row);
      if (row.file) {
        this.imgUrl = row.file.file_url;
      }
      this.ruleForm.id = row.model_number.id;
      this.ruleForm.model_number = row.model_number;
      this.ruleForm.product_ids.push(row.product[0].id)
      this.ruleForm.remark = row.remark
      this.title = "编辑型号";
      this.isProduct = true;
    },
    add_model_number() {
      this.title = "新增型号";
      this.isProduct = true;
      this.imgUrl = "";
      this.ruleForm = {
        id: "",
        model_number: "",
        product_ids: [],
        remark: '',
        file_id: "",
      };
      this.imgUrls = "";
      this.imgUrl = "";
      this.$nextTick(function () {
        this.$refs.Upload.pus_value();
      });
    },
    close_diolog() {
      this.ruleForm = {
        id: "",
        model_number: "",
        product_ids: [],
        file_id: "",
        remark: ''
      };
    },
    submitadd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var isimg = true;
          this.$refs.Upload.$el.children[0].children[0].children.forEach((item) => {
            console.log(item.className);
            if (item.className == "el-upload-list__item is-ready") {
              isimg = false;
            }
          });
          if (isimg == false) {
            this.$message({
              message: "请点击上传到服务器！后重试~",
              type: "error",
            });
            return false;
          }
          addModelNumber(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getlist() {
      getModelNumberList({ ...this.pagination, ...this.ser }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    model_number_click() {
      this.pagination = {
        per_page: 10,
        page: 1,
      };
      getModelNumberList({
        ...this.ser,
        ...this.pagination,
      }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
    del_product(row) {
      delModelNumber({
        id: row.id,
        is_del: 1,
      }).then((res) => {
        if (res.code == 200) {
          this.isProduct = false;
          this.$message.success("提交成功");
          this.getlist();
        }
      });
    }
  },
};
</script>

<style scoped>
.list_order {
  display: grid;
  grid-template-columns: 1fr;
  place-items: left;
}

.list_order .lis {
  width: 100%;
  margin-top: 8px;
}
</style>
